package com.hustzp.com.xichuangzhu.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/8.
 */
public class ChangePasswordActivity extends MyBaseActivity {

    private EditText oldPasw;
    private EditText newPasw;

    private Button submitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        setToolbar();
        initViews();
    }

    private void setToolbar() {
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        navTxt.setText(getString(R.string.account_and_passwd));
        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText(getString(R.string.acount_change_password));
    }

    private void initViews() {
        oldPasw = (EditText) findViewById(R.id.et_old_pasw);
        newPasw = (EditText) findViewById(R.id.et_new_pasw);
        submitBtn = (Button) findViewById(R.id.submit_btn);
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(oldPasw.getText().toString())) {
                    DialogFactory.hintDialog(ChangePasswordActivity.this, getString(R.string.acount_change_old_password_not_empity));
                    return;
                }

                if (TextUtils.isEmpty(newPasw.getText().toString())) {
                    DialogFactory.hintDialog(ChangePasswordActivity.this, getString(R.string.acount_change_new_password_not_empity));
                    return;
                }

                if (oldPasw.getText().toString().length() < 6 || newPasw.getText().toString().length() < 6) {
                    DialogFactory.hintDialog(ChangePasswordActivity.this, getString(R.string.acount_change_new_password_less_than_6));
                    return;
                }

                AVUser.getCurrentUser().updatePasswordInBackground(oldPasw.getText().toString(), newPasw.getText().toString(), new UpdatePasswordCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            // 密码修改成功，重新登录
                            showToastInfo(getString(R.string.acount_change_password_sucessed));
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            DialogFactory.hintDialog(ChangePasswordActivity.this, getString(R.string.acount_change_old_password_error));
                        }
                    }
                });
            }
        });
    }
}
